// Copyright (C) 2022 即时通讯网(52im.net) & Jack Jiang.
// The RainbowChat Project. All rights reserved.
//
// 【本产品为著作权产品，合法授权后请放心使用，禁止外传！】
// 【本次授权给：<广西木子科技有限公司>，授权编号：<NT220402151538>，代码指纹：<A.648883738.885>，技术对接人微信：<ID: Lingmuziyi>】
// 【授权寄送：<收件：李先生、地址：南宁市科园西十路11号国电智能大厦1101F、电话：17736659550、邮箱：yingshashou@vip.qq.com>】
//
// 【本系列产品在国家版权局的著作权登记信息如下】：
// 1）国家版权局登记名(简称)和权证号：RainbowChat    （证书号：软著登字第1220494号、登记号：2016SR041877）
// 2）国家版权局登记名(简称)和权证号：RainbowChat-Web（证书号：软著登字第3743440号、登记号：2019SR0322683）
// 3）国家版权局登记名(简称)和权证号：RainbowAV      （证书号：软著登字第2262004号、登记号：2017SR676720）
// 4）国家版权局登记名(简称)和权证号：MobileIMSDK-Web（证书号：软著登字第2262073号、登记号：2017SR676789）
// 5）国家版权局登记名(简称)和权证号：MobileIMSDK    （证书号：软著登字第1220581号、登记号：2016SR041964）
// * 著作权所有人：江顺/苏州网际时代信息科技有限公司
//
// 【违法或违规使用投诉和举报方式】：
// 联系邮件：jack.jiang@52im.net
// 联系微信：hellojackjiang
// 联系QQ号：413980957
// 授权说明：http://www.52im.net/thread-1115-1-1.html
// 官方社区：http://www.52im.net
package com.x52im.rainbowchat;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.eva.epc.common.file.FileHelper;
import com.eva.epc.common.util.CommonUtils;
import com.eva.epc.common.util.Platform;
import com.eva.epc.common.util.PropertyMan;
import com.eva.framework.utils.LoggerFactory;
import com.x52im.rainbowchat.cache.ConfigCacheProvider;
import com.x52im.rainbowchat.cache.dto.Config4Cache;

public class BaseConf {
    private PropertyMan pm = null;

    private final static String TAG = BaseConf.class.getSimpleName();
    private static BaseConf instance = null;

    /**
     * 邀请好友邮件模板
     */
    private String SNS_INVITE_MAIL_CONTENT_TEMPLATE = null;
    /**
     * 服务端响应用户“忘记密码”请求的邮件模板
     */
    private String SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE = null;
    /**
     * 服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板
     */
    private String SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE = null;

    public static BaseConf getInstance() {
        if (instance == null) {
            instance = new BaseConf();
        }
        return instance;
    }

    public BaseConf() {
        String pathOriginal = BaseConf.class.getResource("base_conf.properties").getPath();
        String path = Platform.isWindows() ? pathOriginal.substring(1) : pathOriginal;

        LoggerFactory.getLog().debug("[" + TAG + "] RainbowChat服务端的核心配置文件路径：" + path);

        pm = new PropertyMan(path);
    }

    /**
     * TODO 本方法以后可考虑收纳到父类中！
     * 因Properties文件默认机制是采用ISO8859-1处理的，所以中文字符读取会存在乱码现象。
     * 本方法是在父类方法的基础上解决乱码问题。
     *
     * @param key
     * @return
     */
    public String getPropertyUTF(String key) {
        try {
            String s = pm.getProperty(key);
            return new String(s.getBytes("ISO-8859-1"), "utf-8");
        } catch (Exception e) {
            LoggerFactory.getLog().error("[" + TAG + "] RainbowChat的配置项：" + key
                    + " 读取失败，请检查您的配置文件路径是否放置正确！", e);
            return null;
        }
    }

    //------------------------------------------------------------ 邀请好友时的邮件配置
    public String getSNS_INVITE_SMTP_SERVER() {
        return getPropertyUTF("SNS_INVITE_SMTP_SERVER");
    }

    public String getSNS_INVITE_SMTP_PORT() {
        return getPropertyUTF("SNS_INVITE_SMTP_PORT");
    }

    public boolean getSNS_INVITE_SMTP_SSL() {
        return Boolean.valueOf(getPropertyUTF("SNS_INVITE_SMTP_SSL"));
    }

    public String getSNS_INVITE_MASTER_EMAIL_ADDR() {
        return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_ADDR");
    }

    public String getSNS_INVITE_MASTER_EMAIL_NAME() {
        return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_NAME");
    }

    public String getSNS_INVITE_MASTER_EMAIL_PASSWORD() {
        return getPropertyUTF("SNS_INVITE_MASTER_EMAIL_PASSWORD");
    }

    public String getSNS_INVITE_MASTER_NICK_NAME() {
        return getPropertyUTF("SNS_INVITE_MASTER_NICK_NAME");
    }

    public String getSNS_INVITE_MAIL_SUBJECT() {
        return getPropertyUTF("SNS_INVITE_MAIL_SUBJECT");
    }

    public String getSNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT() {
        return getPropertyUTF("SNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT");
    }

    public String getSNS_RESPONSE_FORGOT_PSW_ERROR_MAIL_SUBJECT() {
        return getPropertyUTF("SNS_RESPONSE_FORGOT_PSW_ERROR_MAIL_SUBJECT");
    }

    /**
     * 返回好友邀请邮件正文的模板.
     *
     * @return
     */
    private String getSNS_INVITE_MAIL_CONTENT_TEMPLATE() {
        try {
            // 缓存起来，提升性能
            if (SNS_INVITE_MAIL_CONTENT_TEMPLATE == null) {
                SNS_INVITE_MAIL_CONTENT_TEMPLATE = FileHelper.readStringFromFile2(
                        BaseConf.class.getResource("invite_template.html").toURI(), "utf-8");
            }
        } catch (Exception e) {
            LoggerFactory.getLog().warn(e.getMessage(), e);
        }

        return SNS_INVITE_MAIL_CONTENT_TEMPLATE;
    }

    /**
     * 返回填充了内容之后的好友邀请邮件正文.
     *
     * @param fromNickName 目前getSNS_INVITE_MAIL_CONTENT_TEMPLATE()中的模板第0个参数是：发起人的昵称
     * @param fronMail     模板第1个参数是：发起人的mail地址
     * @param fromUid      模板第2个参数是：发起人的uid
     * @return
     */
    public String getSNS_INVITE_MAIL_CONTENT(String fromNickName, String fronMail, String fromUid) {
        String contentTemplate = getSNS_INVITE_MAIL_CONTENT_TEMPLATE();
        if (contentTemplate != null) {
            return MessageFormat.format(contentTemplate, fromNickName, fronMail, fromUid);
        }
        return null;
    }

    /**
     * 返回服务端响应用户“忘记密码”请求的邮件模板.
     *
     * @return
     */
    private String getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE() {
        try {
            // 缓存起来，提升性能
            if (SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE == null) {
                SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE = FileHelper.readStringFromFile2(
                        BaseConf.class.getResource("response_for_forgotpsw_template.html").toURI());
            }
        } catch (Exception e) {
            LoggerFactory.getLog().warn(e.getMessage(), e);
        }

        return SNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE;
    }

    /**
     * 返回填充了内容之后的服务端响应用户“忘记密码”请求的邮件模板.
     *
     * @param nickName 目前getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE()中的模板第0个参数是：昵称
     * @param uid      模板第2个参数是：uid
     * @param mail     模板第3个参数是：mail地址
     * @param password 模板第4个参数是：用户登陆密码
     * @return
     */
    public String getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE(String nickName, String uid, String mail, String password, String path) {
        String contentTemplate = getSNS_RESPONSE_FOR_FORGOTPSW_TEMPLATE();
        if (contentTemplate != null) {
            return MessageFormat.format(contentTemplate
                    , nickName, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), uid, mail, password, path);
        }
        return null;
    }

    /**
     * 返回服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板.
     *
     * @return
     */
    private String getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE() {
        try {
            // 缓存起来，提升性能
            if (SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE == null) {
                SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE = FileHelper.readStringFromFile2(
                        BaseConf.class.getResource("response_for_forgotpsw_error_template.html").toURI());
            }
        } catch (Exception e) {
            LoggerFactory.getLog().warn(e.getMessage(), e);
        }

        return SNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE;
    }

    /**
     * 返回填充了内容之后的服务端响应用户“忘记密码”请求（没有查到账户信息）的邮件模板.
     *
     * @param fromMail 模板第1个参数是：忘记密码请求的目标邮箱地址
     * @return
     */
    public String getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE(String fromMail) {
        String contentTemplate = getSNS_RESPONSE_FOR_FORGOTPSW_ERROR_TEMPLATE();
        if (contentTemplate != null) {
            return MessageFormat.format(contentTemplate
                    , new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), fromMail);
        }
        return null;
    }

    public String getDIR_USER_AVATAR_UPLOAD() {
        return getPropertyUTF("DIR_USER_AVATAR_UPLOAD");
    }

    public String getDIR_USER_AVATARGROUP_DIR() {
        return getPropertyUTF("DIR_USER_AVATARGROUP_DIR");
    }

    public String getDIR_USER_IMAGE_UPLOAD_UNREAD() {
        return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_UNREAD");
    }

    public String getDIR_USER_IMAGE_UPLOAD_READ() {
        return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_READ");
    }

    public String getDIR_USER_IMAGE_UPLOAD_TIMEOUT() {
        return getPropertyUTF("DIR_USER_IMAGE_UPLOAD_TIMEOUT");
    }

    public String getDIR_USER_VOICE_UPLOAD_UNREAD() {
        return getPropertyUTF("DIR_USER_VOICE_UPLOAD_UNREAD");
    }

    public String getDIR_USER_VOICE_UPLOAD_READ() {
        return getPropertyUTF("DIR_USER_VOICE_UPLOAD_READ");
    }

    public String getDIR_USER_VOICE_UPLOAD_TIMEOUT() {
        return getPropertyUTF("DIR_USER_VOICE_UPLOAD_TIMEOUT");
    }

    public String getDIR_USER_VOICE_UPLOAD_TO_MP3() {
        return getPropertyUTF("DIR_USER_VOICE_UPLOAD_TO_MP3");
    }

    public String getDIR_USER_BIGFILE_UPLOAD() {
        return getPropertyUTF("DIR_USER_BIGFILE_UPLOAD");
    }

    public String getDIR_USER_SHORTVIDEO_UPLOAD() {
        return getPropertyUTF("DIR_USER_SHORTVIDEO_UPLOAD");
    }

    public String getDIR_USER_SHORTVIDEO_UPLOAD_TIMEOUT() {
        return getPropertyUTF("DIR_USER_SHORTVIDEO_UPLOAD_TIMEOUT");
    }

    public String getAUTO_TIMING_RESTORE_IMG_AND_VOICE_INTERVAL() {
        return getPropertyUTF("AUTO_TIMING_RESTORE_IMG_AND_VOICE_INTERVAL");
    }

    public String getAUTO_TIMING_RESTORE_IMG_AND_VOICE_TIMEOUT() {
        return getPropertyUTF("AUTO_TIMING_RESTORE_IMG_AND_VOICE_TIMEOUT");
    }

    public String getDIR_USER_PHOTO_UPLOAD() {
        return getPropertyUTF("DIR_USER_PHOTO_UPLOAD");
    }

    public String getDIR_USER_PVOICE_UPLOAD() {
        return getPropertyUTF("DIR_USER_PVOICE_UPLOAD");
    }

    public String getDIR_USER_LOCATION_UPLOAD_UNREAD() {
        return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_UNREAD");
    }

    public String getDIR_USER_LOCATION_UPLOAD_READ() {
        return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_READ");
    }

    public String getDIR_USER_LOCATION_UPLOAD_TIMEOUT() {
        return getPropertyUTF("DIR_USER_LOCATION_UPLOAD_TIMEOUT");
    }

    public String getDIR_USER_REMARK_PHOTO_UPLOAD() {
        return getPropertyUTF("DIR_USER_REMARK_PHOTO_UPLOAD");
    }

    public boolean getIM_BRIDGE_ENABLED() {
        return Boolean.valueOf(getPropertyUTF("IM_BRIDGE_ENABLED"));
    }

    public String getIM_BRIDGE_MQ_URI() {
        return getPropertyUTF("IM_BRIDGE_MQ_URI");
    }

    public int getIM_SERVER_UDP_LISTENNING_PORT() {
        return CommonUtils.getIntValue(getPropertyUTF("IM_SERVER_UDP_LISTENNING_PORT")
                , 9903);
    }

    public int getIM_SERVER_TCP_LISTENNING_PORT() {
        return CommonUtils.getIntValue(getPropertyUTF("IM_SERVER_TCP_LISTENNING_PORT")
                , 9903);
    }

    public boolean getIM_SERVER_UDP_SUPPORT() {
        return Boolean.valueOf(getPropertyUTF("IM_SERVER_UDP_SUPPORT"));
    }

    public boolean getIM_SERVER_TCP_SUPPORT() {
        return Boolean.valueOf(getPropertyUTF("IM_SERVER_TCP_SUPPORT"));
    }

    public String getAUTO_TIMING_RESTORE_MESSAGES_INTERVAL() {
        return getPropertyUTF("AUTO_TIMING_RESTORE_MESSAGES_INTERVAL");
    }

    public String getAUTO_TIMING_RESTORE_MESSAGES_TIMEOUT() {
        return getPropertyUTF("AUTO_TIMING_RESTORE_MESSAGES_TIMEOUT");
    }

    public String getSMS_ACCESSKEY_ID() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.sms, "accesskey_id");
//        return getPropertyUTF("SMS_ACCESSKEY_ID");
    }

    public String getSMS_ACCESSKEY_SECRET() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.sms, "accesskey_secret");
//        return getPropertyUTF("SMS_ACCESSKEY_SECRET");
    }

    public String getSMS_SIGNNAME() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.sms, "signname");
//        return getPropertyUTF("SMS_SIGNNAME");
    }

    public String getSMS_TEMPLATECODE() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.sms, "templatecode");
//        return getPropertyUTF("SMS_TEMPLATECODE");
    }

    public String getAPP_ID() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "app_id");
//        return getPropertyUTF("APP_ID");
    }

    public String getAPP_privateKey() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "app_privateKey");
//        return getPropertyUTF("APP_privateKey");
    }

    public String getAPP_certPublicKey() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "app_certPublicKey");
//        return getPropertyUTF("APP_certPublicKey");
    }

    public String getALI_rootPath() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "ali_rootPath");
//        return getPropertyUTF("ALI_rootPath");
    }

    public String getALI_certPublicKey() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "ali_certPublicKey");
//        return getPropertyUTF("ALI_certPublicKey");
    }

    public String getAPP_ALIPAY_USER_ID() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "app_user_id");
//        return getPropertyUTF("APP_ALIPAY_USER_ID");
    }

    public String getALI_CERT_REDIRECT_URL() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "cert_redirect_url");
//        return getPropertyUTF("ALI_CERT_REDIRECT_URL");
    }

    public String getALI_DEPOSIT_REDIRECT_URL() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "deposit_redirect_url");
//        return getPropertyUTF("ALI_DEPOSIT_REDIRECT_URL");
    }


    public String getREDIS_HOST() {
        return getPropertyUTF("REDIS_HOST");
    }

    public int getREDIS_PORT() {
        return CommonUtils.getIntValue(getPropertyUTF("REDIS_PORT"));
    }

    public int getREDIS_TIMEOUT() {
        return CommonUtils.getIntValue(getPropertyUTF("REDIS_TIMEOUT"));
    }

    public String getREDIS_PASSWORD() {
        return getPropertyUTF("REDIS_PASSWORD");
    }

    public String getREDIS_TOKEN_EXPIRETIME() {
        return getPropertyUTF("REDIS_TOKEN_EXPIRETIME");
    }

    public String getFILTER_TOKEN_INF() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.notoken, "infs");
//        return getPropertyUTF("FILTER_TOKEN_INF");
    }

    public String getALI_CASH_LIMIT() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "cash_limit");
    }

    public String getALI_CASH_RATE() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.alipay, "cash_rate");
    }

    public boolean getLOG_FLAG() {
        String logFlag = ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.log, "oper_flag");
        if (CommonUtils.isStringEmpty(logFlag)) {
            logFlag = "false";
        }
        return logFlag.equalsIgnoreCase("true") ? true : false;

//        String flag = getPropertyUTF("LOG_FLAG");
//        return flag.equalsIgnoreCase("true") ? true : false;
    }

    public boolean getLOG_FLAG_INTERFACE() {
        String logFlag = ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.log, "interface_flag");
        if (CommonUtils.isStringEmpty(logFlag)) {
            logFlag = "false";
        }
        return logFlag.equalsIgnoreCase("true") ? true : false;

//        String flag = getPropertyUTF("LOG_FLAG_INTERFACE");
//        return flag.equalsIgnoreCase("true") ? true : false;
    }

    public String getAPK_VERSION() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.apk, "apk_version");
    }

    public String getAPK_NAME() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.apk, "apk_name");
    }

    public String getAPK_URL() {
        return ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.apk, "apk_url");
    }

    public Integer getGROUP_MAX_ADMIN() {
        String groupMaxAdmin = ConfigCacheProvider.getInstance().getValue(Config4Cache.TYPE.group, "group_max_admin");
        return CommonUtils.getIntValue(groupMaxAdmin);
    }

//	public static void main(String[] args)
//	{
//		BaseConf bc = new BaseConf();
//		System.out.println(bc.getSNS_INVITE_SMTP_SERVER());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_ADDR());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_NAME());
//		System.out.println(bc.getSNS_INVITE_MASTER_EMAIL_PASSWORD());
//		System.out.println(bc.getSNS_INVITE_MASTER_NICK_NAME());
//		System.out.println(bc.getSNS_INVITE_MAIL_SUBJECT());
//		System.out.println(bc.getDIR_USER_AVATAR_UPLOAD());
//
//		try
//		{
//			String s = FileHelper.readStringFromFile(
//					BaseConf.class.getResource("invite_template.html").toURI());
//			String tt = MessageFormat.format(s,"Jack Jiang","abcc@163.com","4244555441");
//			System.out.println("tt="+tt);
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		BaseConf bc = new BaseConf();
//		bc.getSNS_RESPONSE_FORGOT_PSW_MAIL_SUBJECT();
//	}
}
